package com.flainsky.nirvanaplayer;

import java.util.List;

import org.videolan.vlc.android.VideoPlayerActivity;

import com.flainsky.nirvanaplayer.globe.DownloadUtils;
import com.flainsky.nirvanaplayer.globe.HistroyController;
import com.flainsky.nirvanaplayer.globe.PlayController;
import com.flainsky.nirvanaplayer.globe.DownloadUtils.DataType;
import com.flainsky.nirvanaplayer.lib.objects.Movie;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryActivity extends Activity implements OnClickListener {
	
	List<Movie> hList;
	ListView hListView;
	Button clearBt;
	Button cancelBt;
	HistroyAdapter hAdapter;
	
	ProgressDialog pdialog;	//进度条
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.histroy_activity);
        
        findViews();
        addListeners();
        init();
	}
	
    @Override
    public void onDestroy()
    {
    	pdialog.dismiss();
    	super.onDestroy();
    }
	
	private void findViews()
	{
		hListView = (ListView)findViewById(R.id.history_ls);
		clearBt = (Button)findViewById(R.id.clear_all_bt);
		cancelBt = (Button)findViewById(R.id.cancel_bt);
	}
	private void addListeners()
	{
		clearBt.setOnClickListener(this);
		cancelBt.setOnClickListener(this);
	}
	
	private void init()
	{
		hAdapter = new HistroyAdapter();
		hList = HistroyController.getInstance().getHistroy();
		hListView.setAdapter(hAdapter);
		
		pdialog = new ProgressDialog(HistoryActivity.this);
    	pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
	         public void onClick(DialogInterface dialog, int i) {
	        	 dialog.cancel();
	         }
	    });
    	pdialog.setTitle(R.string.wait);
        pdialog.setCancelable(true);
        pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	}
	
	private class HistroyAdapter extends BaseAdapter
	{
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return hList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return hList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return hList.get(arg0).mId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView == null)
			{
				LayoutInflater inflater=LayoutInflater.from(HistoryActivity.this);
				convertView = inflater.inflate(R.layout.history_item, null);
			}
			final Movie info = hList.get(position);
			Button delBt = (Button)convertView.findViewById(R.id.history_del);
			delBt.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					HistroyController.getInstance().clearItem(info);
					HistroyAdapter.this.notifyDataSetChanged();
				}
			});
			
			TextView tv = (TextView)convertView.findViewById(R.id.history_title);
			Button playBt = (Button)convertView.findViewById(R.id.history_play);
			playBt.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					PlayController.getInstance().setCurrentPlayMovie(info);
					LoadingVideoAsyncTask lvat = new LoadingVideoAsyncTask();
					lvat.execute();
					HistroyAdapter.this.notifyDataSetChanged();
				}
			});
			
			tv.setText(info.mTitle);
			return convertView;
		}
	}
	
	private void play()
	{
		Intent intent = new Intent(this,VideoPlayerActivity.class);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
		case R.id.cancel_bt:
			this.finish();
			return;
		case R.id.clear_all_bt:
			if(hList!=null)
			{
				hList.clear();
				hAdapter.notifyDataSetChanged();
			}
			return;
		}
	}
	
	public class LoadingVideoAsyncTask extends AsyncTask<Void, Void, Void> {
		boolean isNotExit = false;
		public LoadingVideoAsyncTask()
		{
			pdialog.setMessage(HistoryActivity.this.getString(R.string.video_loading));
		}
		
		@Override
		protected void onPreExecute()
		{
			if(pdialog!=null && !pdialog.isShowing())
			{
				pdialog.show();
			}
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			isNotExit = !DownloadUtils.downloadFile(HistoryActivity.this,PlayController.getInstance().getCurrentPlayUrl(), DataType.movie);
			return null;
		}
		
		@Override
	    protected void onPostExecute(Void result) {
			if(pdialog != null && pdialog.isShowing())
			{
				pdialog.dismiss();
			}
			if(isNotExit)
			{
				Toast.makeText(HistoryActivity.this, PlayController.getInstance().getCurrentPlayUrl()+" file not exits in server!",Toast.LENGTH_LONG).show();
				return;
			}
			play();
		}
	}
}
